﻿using Freejobber.Core.Generic.Profile;
using Freejobber.Core.Generic.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Security.Application;

namespace Freejobber.Profile
{
    public partial class PersonalInfo : System.Web.UI.Page
    {
        UserClass _user = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            ImageUploader.UserLogin = "gelleor";
            InitPage();
        }

        private void InitPage()
        {
            int _userID = -1;
            if (!SecurityManager.IsUserLogon())
                Response.Redirect("~/");
            _user = SecurityManager.GetCurrentUser();
            Page.Title = "Кабинет|Личные данные " + _user.UserName;
            if (!IsPostBack)
            {
                InitPersonalControls(_userID);
            }
        }

        private void InitPersonalControls(int _userID)
        {

            tbFullName.Text = Server.HtmlDecode(_user.UserName);
            tbDescriptions.Text = _user.Description;
            tbEmail.Text = _user.Email;
            tbPhoneOther.Text = _user.Phone2;
            tbPhone.Text = _user.Phone1;
            hlPersonalPage.NavigateUrl = string.Format("http://www.freejobber.ru/profile/userprofile.aspx?name={0}",_user.LoginName);
            rbWorkForm.SelectedValue= _user.WorkForm.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveProfile();
        }

        private void SaveProfile()
        {
            bool _isChanged = false;

            //FIO
            if (!string.IsNullOrEmpty(Encoder.HtmlEncode(tbFullName.Text)) && !Encoder.HtmlEncode(tbFullName.Text).Equals(_user.UserName))
            {
                _isChanged = true;
                _user.UserName = Encoder.HtmlEncode(tbFullName.Text);
               // _user.UserName = tbFullName.Text;
            }

            //Email
            if (!string.IsNullOrEmpty(Encoder.HtmlEncode(tbEmail.Text)) && !Encoder.HtmlEncode(tbEmail.Text).Equals(_user.Email))
            {
                _isChanged = true;
                _user.Email = Encoder.HtmlEncode(tbEmail.Text);
            }

            //Description
            if (!string.IsNullOrEmpty(Encoder.HtmlEncode(tbDescriptions.Text)) && !Encoder.HtmlEncode(tbDescriptions.Text).Equals(_user.Description))
            {
                _isChanged = true;
                _user.Description = Encoder.HtmlEncode(tbDescriptions.Text);
            }

            //Phone
            if (!string.IsNullOrEmpty(Encoder.HtmlEncode(tbPhone.Text)) && !Encoder.HtmlEncode(tbPhone.Text).Equals(_user.Phone1))
            {
                _isChanged = true;
                _user.Phone1 = Encoder.HtmlEncode(tbPhone.Text);
            }

            //Other phones
            if (!string.IsNullOrEmpty(Encoder.HtmlEncode(tbPhoneOther.Text)) && !Encoder.HtmlEncode(tbPhoneOther.Text).Equals(_user.Phone2))
            {
                _isChanged = true;
                _user.Phone2 = Encoder.HtmlEncode(tbPhoneOther.Text);
            }

            //Work form
            byte _workForm;
            byte.TryParse(rbWorkForm.SelectedValue, out _workForm);
            _user.WorkForm = _workForm;

            if (_isChanged)
                _user.Save();
        }


        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            byte[] _data= e.GetContents();
        }
    }
}